package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.POJO.CheckItem;
import com.itheima.dao.CheckitemDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.service.CheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @Description:
 * @Version: V1.0
 */

@Service(interfaceClass = CheckitemService.class)
@Transactional
public class CheckitemServiceImpl implements CheckitemService {
    @Autowired
    private CheckitemDao checkitemDao;

    @Override
    public void add(CheckItem checkItem) {
        checkitemDao.add(checkItem);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();

        PageHelper.startPage(currentPage,pageSize);
        Page<CheckItem> list=checkitemDao.findPage(queryString);

        PageResult pageResult=new PageResult(list.getTotal(),list.getResult());
        return pageResult;
    }

    @Override
    public CheckItem findById(int id) {
        return checkitemDao.findById(id);
    }

    @Override
    public void edit(CheckItem checkItem) {
        checkitemDao.edit(checkItem);
    }

    @Override
    public void delete(int id) {
        int count= checkitemDao.findCountByCheckItemId(id);
        if (count>0){
            throw new RuntimeException("当前检查项被引用,不能删除");
        }
        checkitemDao.delete(id);

    }

    @Override
    public List<CheckItem> findAll() {
        List<CheckItem> list=checkitemDao.findAll();

        return list;
    }
}
